package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.internal.component.DaggerForgetPasswordComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.ForgetPasswordModule;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.presenter.ForgetPasswordPresenter;
import com.zw.yzk.kabu.utils.GetRsaString;
import com.zw.yzk.kabu.view.view.ForgetPasswordView;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2016/12/25.
 */

public class ForgetPasswordActivity extends BaseActivity implements ForgetPasswordView {

    @Bind(R.id.phone)
    EditText phone;
    @Bind(R.id.verify_code)
    EditText verifyCode;
    @Bind(R.id.first_password)
    EditText firstPassword;
    @Bind(R.id.get_verify_code)
    TextView getVerifyCode;

    @Inject
    ForgetPasswordPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.get_back_password))
                .setBackgroundColor(ContextCompat.getColor(this, R.color.transparent))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerForgetPasswordComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .forgetPasswordModule(new ForgetPasswordModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getPasswordSuccess() {
        ToastManager.getInstance().showToast(this, getResources().getString(R.string.reset_password_success));
        finish();
    }

    @Override
    public void getPasswordFail() {
        ToastManager.getInstance().showToast(this, getResources().getString(R.string.reset_password_fail));
    }

    @Override
    public void countDown(long count) {
        getVerifyCode.setText(getResources().getString(R.string.left_seconds, count));
    }

    @Override
    public void setGetVerifyCodeBtnStatus(boolean enable) {
        if (enable) {
            getVerifyCode.setText(getResources().getString(R.string.get_verify_code));
        }
        getVerifyCode.setEnabled(enable);
    }

    @OnClick(R.id.get_verify_code)
    public void getVerifyCode() {
        String phoneNum = phone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_phone_number));
        } else {
            presenter.getVerifyCode(phoneNum);
        }
    }

    @OnClick(R.id.submit)
    public void resetPassword() {
        String phoneNum = phone.getText().toString();
        String verifyCoeStr = verifyCode.getText().toString();
        String firstPasswordStr = firstPassword.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_phone_number));
        } else if (TextUtils.isEmpty(verifyCoeStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_verify_code));
        } else if (TextUtils.isEmpty(firstPasswordStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_password));
        } else {
            resetPassword(phoneNum, firstPasswordStr, verifyCoeStr);
        }
    }

    /**
     * 重设密码
     *
     * @param phone      手机
     * @param verifyCode 验证码
     * @param password   密码
     */
    private void resetPassword(String phone, String password, String verifyCode) {
        presenter.register(phone, new GetRsaString(this, password).getRsa(), verifyCode);
    }

}
